package org.snail.web.config;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.snail.activemq.converter.DelayMessageConverter;
import org.snail.activemq.listener.AbAttendenceListener;
import org.snail.activemq.listener.AbnormalSenderListener;
import org.snail.activemq.listener.AttendenceListener;
import org.snail.attendence.service.listener.AttendanceInfoListener;
import org.snail.attendence.service.listener.AttendenceMongoListener;
import org.snail.chat.service.listener.ChainToChatListener;
import org.snail.department.service.listener.DeptInfoListener;
import org.snail.employee.service.listener.EmployeesListener;
import org.snail.message.service.listener.GroupMessageSenderListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import javax.jms.MessageListener;

/**
 * Created by WJ on 2017/12/8.
 */
@Configuration
@PropertySource("classpath:context-datasource.properties")
public class JmsElConfig {

    @Value("${activeMq}")
    private String activeMq;
    @Value("${maxActivemqPoolSize}")
    private int maxActivemqPoolSize;

    //    下面是单个消息sender和consumer的名称配置
    //异常考勤数据展示
    @Value("abAttendenceMongoSender")
    private String abAttendenceMongoSender;
    @Value("abAttendenceMongoConsumer")
    private String abAttendenceMongoConsumer;
    //考勤数据展示
    @Value("attendenceMongoSender")
    private String attendenceMongoSender;
    @Value("attendenceMongoConsumer")
    private String attendenceMongoConsumer;
    //异常消息发送放到队列里面试试
    @Value("attendenceAbnormalSender")
    private String abnormalSender;
    @Value("attendenceAbnormalConsumer")
    private String attendenceAbnormalConsumer;
    //考勤
    @Value("attendenceSender")
    private String attendenceSender;
    @Value("attendenceConsumer")
    private String attendenceConsumer;
    @Value("groupMessageSender")                    //群发送监听器
    private String groupMessageSender;
    @Value("groupMessageConsumer")
    private String groupMessageConsumer;


    //保存钉钉接口 中获取的部门信息
    @Value("deptInfoSender")
    private String deptInfoSender;
    //保存钉钉接口 中获取人员信息相关对列
    @Value("employeeSender")
    private String employeeSender;
    //保存部门人员的信息
    @Value("chainToChatSender")
    private String chainToChatSender;

    /**
     * Jms 配置
     * @return 返回的是activemq连接工厂       =========================================================================================
     */
    @Bean(name="activeMQConnectionFactory")
    public ActiveMQConnectionFactory activeMQConnectionFactory()
    {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setUseAsyncSend(true);
        activeMQConnectionFactory.setMaxThreadPoolSize(maxActivemqPoolSize);
        activeMQConnectionFactory.setBrokerURL(activeMq);
        return activeMQConnectionFactory;
    }

    @Bean(name="cachingConnectionFactory")
    public CachingConnectionFactory cachingConnectionFactory(ActiveMQConnectionFactory targetConnectionFactory)
    {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(targetConnectionFactory);
        cachingConnectionFactory.setSessionCacheSize(100);
        return cachingConnectionFactory;
    }

    @Bean(name="delayMessageConverter")
    public DelayMessageConverter delayMessageConverter(){
        DelayMessageConverter delayMessageConverter = new DelayMessageConverter();
        return delayMessageConverter;
    }

    @Bean(name = "jmsTemplate")
    public JmsTemplate jmsTemplate(CachingConnectionFactory cachingConnectionFactory){
        JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
        jmsTemplate.setMessageConverter(delayMessageConverter());
        return jmsTemplate;
    }

    @Bean(name="groupMessageSenderQueue")
    public ActiveMQQueue groupMessageSenderQueue(){
        return new ActiveMQQueue(groupMessageSender);
    }
    @Bean(name="groupMessageSenderConsumer")
    public DefaultMessageListenerContainer groupMessageSenderConsumer(CachingConnectionFactory cachingConnectionFactory, ActiveMQQueue groupMessageSenderQueue, GroupMessageSenderListener groupMessageSenderListener){
        return defaultMessageListenerContainer(cachingConnectionFactory, groupMessageSenderQueue, groupMessageSenderListener);
    }

    @Bean(name="abnormalSenderQueue")
    public ActiveMQQueue abnormalSenderQueue(){
        return new ActiveMQQueue(abnormalSender);
    }
    @Bean(name="abnormalSenderConsumer")
    public DefaultMessageListenerContainer attendenceAbnormalConsumer(CachingConnectionFactory cachingConnectionFactory, ActiveMQQueue abnormalSenderQueue, AbnormalSenderListener abnormalSenderListener){
        return defaultMessageListenerContainer(cachingConnectionFactory, abnormalSenderQueue, abnormalSenderListener);
    }

    /**
     * 考勤消息队列
     * @return
     */
    @Bean(name="attendenceSenderQueue")
    public ActiveMQQueue attendenceSenderQueue(){
        return new ActiveMQQueue(attendenceSender);
    }
    @Bean(name="attendenceConsumer")
    public DefaultMessageListenerContainer attendenceConsumer(CachingConnectionFactory cachingConnectionFactory, ActiveMQQueue attendenceSenderQueue, AttendanceInfoListener attendanceInfoListener){
        return defaultMessageListenerContainer(cachingConnectionFactory, attendenceSenderQueue, attendanceInfoListener);
    }
    /**
     * activeMq推送的消息
     * 将异常考勤存入mongoDB,推送至消息队列
     */
    //TODO  之前是将重复代码提交了，但是奇怪的是本地编译是可以通过的，但是linux上不行，所以我还是改回来了 后续了解什么我问题
    @Bean(name = "abAttendenceMongoQueue")
    public ActiveMQQueue abAttendenceMongoQueue(){
        return new ActiveMQQueue(abAttendenceMongoSender);
    }
    @Bean(name = "abAttendenceMongoConsumer")
    public DefaultMessageListenerContainer abAttendenceMongoConsumer(CachingConnectionFactory cachingConnectionFactory, ActiveMQQueue abAttendenceMongoQueue, AbAttendenceListener abAttendenceListener){
        return defaultMessageListenerContainer(cachingConnectionFactory, abAttendenceMongoQueue, abAttendenceListener);
    }

    /**
     * activemq attendence保存至数据库
     */
    @Bean(name = "attendenceMongoQueue")
    public ActiveMQQueue attendenceMongoQueue(){
        return new ActiveMQQueue(attendenceMongoSender);
    }
    @Bean(name = "attendenceMongoConsumer")
    public DefaultMessageListenerContainer attendenceMongoConsumer(CachingConnectionFactory cachingConnectionFactory, ActiveMQQueue attendenceMongoQueue, AttendenceMongoListener attendenceListener){
        return defaultMessageListenerContainer(cachingConnectionFactory, attendenceMongoQueue, attendenceListener);
    }

    /**
     * 部门人员列表
     * @return active 队列
     */
    @Bean(name = "chainToChatQueue")
    public ActiveMQQueue chainToChatQueue(){
        return new ActiveMQQueue(chainToChatSender);
    }
    @Bean(name = "chainToChatConsumer")
    public DefaultMessageListenerContainer chainToChatConsumer(CachingConnectionFactory cachingConnectionFactory, ActiveMQQueue chainToChatQueue, ChainToChatListener chainToChatListener){
        return defaultMessageListenerContainer(cachingConnectionFactory, chainToChatQueue, chainToChatListener);
    }


    /**
     * 人员信息同步队列
     */
    @Bean(name = "employeesQueue")
    public ActiveMQQueue employeesQueue(){
        return new ActiveMQQueue(employeeSender);
    }
    @Bean(name = "employeesConsumer")
    public DefaultMessageListenerContainer employeesConsumer(CachingConnectionFactory cachingConnectionFactory, ActiveMQQueue employeesQueue, EmployeesListener employeesListener){
        return defaultMessageListenerContainer(cachingConnectionFactory, employeesQueue, employeesListener);
    }


    /**
     * activemq 将dept的信息保存至数据库中
     */
    @Bean(name = "deptInfoQueue")
    public ActiveMQQueue deptInfoQueue(){
        return new ActiveMQQueue(deptInfoSender);
    }
    @Bean(name = "deptInfoConsumer")
    public DefaultMessageListenerContainer deptInfoConsumer(CachingConnectionFactory cachingConnectionFactory, ActiveMQQueue deptInfoQueue, DeptInfoListener deptInfoListener){
        return defaultMessageListenerContainer(cachingConnectionFactory, deptInfoQueue, deptInfoListener);
    }

    /**
     *  监听容器公用生产方法
     * @param cachingConnectionFactory
     * @param chainToChatQueue
     * @param messageListener
     * @return
     */
    private DefaultMessageListenerContainer defaultMessageListenerContainer(CachingConnectionFactory cachingConnectionFactory, ActiveMQQueue chainToChatQueue, MessageListener messageListener) {
        DefaultMessageListenerContainer defaultMessageListenerContainer = new DefaultMessageListenerContainer();
        defaultMessageListenerContainer.setConnectionFactory(cachingConnectionFactory);
        defaultMessageListenerContainer.setDestination(chainToChatQueue);
        defaultMessageListenerContainer.setMessageListener(messageListener);
        return defaultMessageListenerContainer;
    }


}
